package com.aisilesn.base.service;

import com.aisilesn.base.dao.CityDao;
import com.aisilesn.base.pojo.City;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @Author: Mr.liu
 * @Description:
 * @Date: Create in 9:19 2019/4/24
 */
@Service
public class CityService {
    @Autowired
    CityDao cityDao;

    @Autowired
    IdWorker idWorker;

    /**
     * 查询所有城市信息
     * @return
     */
    public List<City> findAll() {
        List<City> all = cityDao.findAll();
        return all;
    }

    /**
     * 根据ID查询城市
     * @param id
     * @return
     */
    public City findById(String id) {
        City city = cityDao.findById(id).get();
        return city;

    }

    public void add(City city) {
        city.setId(idWorker.nextId()+"");
        cityDao.save(city);
    }

    public void update(City city) {
        cityDao.save(city);
    }

    public void deleteById(String id) {
        cityDao.deleteById(id);
    }

    /**
     * 条件查询
     * @param searchMap
     * @return
     */
    public List<City> findSearch(Map searchMap) {
        List<City> all = cityDao.findAll(getSpecification(searchMap));
        return all;
    }

    public Page<City> findSearchPage(Map searchMap, int page, int size) {
        PageRequest pageRequest = PageRequest.of(page,size);
        Page<City> all = cityDao.findAll(getSpecification(searchMap), pageRequest);
        return all;
    }

    /**
     * 拼装查询条件
     * @param searchMap
     * @return
     */
    public Specification<City> getSpecification(Map searchMap){
       return new Specification<City>() {
            @Override
            public Predicate toPredicate(Root<City> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //拼条件
                List<Predicate> list = new ArrayList<>();
                String id = (String) searchMap.get("id");//ID
                String name = (String) searchMap.get("name");//城市名字
                String ishot = (String) searchMap.get("ishot");//是否热门
                if(!StringUtils.isEmpty(id)){
                    list.add(cb.equal(root.get("id"),id));//添加条件
                }
                if(!StringUtils.isEmpty(name)){
                    list.add(cb.like(root.get("name"),"%"+name+"%"));//添加条件
                }
                if(!StringUtils.isEmpty(ishot)){
                    list.add(cb.equal(root.get("ishot"),ishot));//添加条件
                }
                Predicate and = cb.and(list.toArray(new Predicate[list.size()]));
                return and;
            }
        };
    }
}
